#include<p18cxxx.h>
#include"config.h"


extern unsigned int adc_value[3];
//channel 0, channel 1, channel 2 will goto corresponding arrays 0, 1 & 2

void init_ADC(void);
void ADC_scan(void);




void init_ADC(void)
{
ADCON0=0;
ADCON1=0b00001100;		//enable AN0, AN1, AN2 as analog channel with VSS and VDD as reference
ADCON2=0b10100100;		//right justified, with Tad= Fosc/4 and acquisition time 8 Tad
ADCON0bits.ADON=1;		//turn on the ADC module
PIR1bits.ADIF=0;		//clear the ADC interrupt flag
PIE1bits.ADIE=1;		//enable the ADC interrupt
INTCONbits.PEIE=1;		//enable peripheral interrupts
INTCONbits.GIE=1;		//enable global interrupts
}


void ADC_scan(void)		
{
unsigned char temp;
temp=ADCON0bits.CHS;
adc_value[temp]=(ADRESH*256) + ADRESL;		//combine the converted ADC values and write to corresponding adc buffer

if(temp>1)		//switch to the next channel
temp=0;
else
temp++;
ADCON0bits.CHS=temp;
return;
}
